package com.hxd.mall.utils.open

import android.text.Editable
import android.text.TextWatcher
import android.view.View
import android.widget.EditText
import android.widget.TextView
import androidx.viewpager.widget.ViewPager
import com.hxd.mall.R
import com.hxd.mall.constant.HxdGlobalInfo

/**
 * Listener扩展事件
 */
/**
 * textWatcher监听
 */
//fun addTextWatcherListener(editText: EditText, callback: (String) -> Unit) {
//    editText.addTextChangedListener(object : TextWatcher {
//        override fun afterTextChanged(s: Editable?) {
//            callback(s?.toString() ?: "")
//        }
//        override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
//        }
//        override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
//        }
//    })
//}

fun addTextWatcherListener(textView: TextView, callback: (String) -> Unit) {
    textView.addTextChangedListener(object : TextWatcher {
        override fun afterTextChanged(s: Editable?) {
            callback(s?.toString() ?: "")
        }

        override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

        }

        override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
        }
    })
}

fun EditText.addFocusListener(hasFocus: (() -> Unit)?, cancelFocus: (() -> Unit)?) {
    setOnFocusChangeListener { _, hasFocu ->
        if (hasFocu) {
            if (hasFocus != null) hasFocus()
        } else
            if (cancelFocus != null) cancelFocus()
    }
}

/**
 * addSetOnClickListener
 */
fun addSetOnClickListener(vararg views: View, value: String? = "", callback: (View) -> Unit) {
    views.forEach {
        it.setOnClickListener {
            if (FaskClickUitl.isFastClick()) {
                return@setOnClickListener
            }
            if (!NetUtil.isNetworkAvailable(HxdGlobalInfo.instance)) {
                ToastUtil.showToast(HxdGlobalInfo.instance.getString(R.string.net_error_tip))
                return@setOnClickListener
            }
            callback(it)
            //setBuriedPoint(it)
        }
    }
}

/**
 * addSetOnClickListener
 */
//fun addSetOnClickListenerApply(vararg views: View, callback: (View) -> Unit) {
//    views.forEach {
//        it.setOnClickListener {
//            if (FaskClickUitl.isFastClick()) {
//                return@setOnClickListener
//            }
//            if (!NetUtil.isNetworkAvailable(HxdGlobalInfo.instance)) {
//                ToastUtil.showToast(HxdGlobalInfo.instance.getString(R.string.net_error_tip))
//                return@setOnClickListener
//            }
//            callback(it)
//            setBuriedPoint(it)
//        }
//    }
//}

/**
 * addOnPageChangeListener
 */
fun addOnPageChangeListener(view: ViewPager, callback: (Int) -> Unit) {
    view.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
        override fun onPageSelected(p0: Int) {
            callback(p0)
        }

        override fun onPageScrollStateChanged(p0: Int) {
        }

        override fun onPageScrolled(p0: Int, p1: Float, p2: Int) {
        }
    })
}